package com.zjdiepu.www.biz.dz;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dz.DzLoanMiddleAuditMapper;
import com.zjdiepu.www.dao.dz.DzLoanMiddleAuditSubMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.ibiz.dz.IDzLoanMiddleAuditService;
import com.zjdiepu.www.model.dz.dto.DzLoanMiddleAudit;
import com.zjdiepu.www.model.dz.dto.DzLoanMiddleAuditSub;
import com.zjdiepu.www.model.dz.ext.DzLoanMiddleAuditExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月02日 13时29分<br/>
 * @since 1.0<br/>
 */
@Service
public class DzLoanMiddleAuditServiceImpl extends BaseService implements IDzLoanMiddleAuditService {
	public static final Logger logger = LoggerFactory.getLogger(DzLoanMiddleAuditServiceImpl.class);

	@Autowired
	private DzLoanMiddleAuditMapper dzLoanMiddleAuditMapper;
	@Autowired 
	private YwBusinessMapper ywBusinessMapper;
	
	@Autowired
	private DzLoanMiddleAuditSubMapper dzLoanMiddleAuditSubMapper;
	
	
	@Override
	public void saveUpdateAndDelete(DzLoanMiddleAudit dzLoanMiddleAudit, OperateType operType) {
		switch (operType) {
		case add:
			dzLoanMiddleAudit.initInsertData(dzLoanMiddleAudit.getModifier(), dzLoanMiddleAudit.getModifierName());
			dzLoanMiddleAuditMapper.insert(dzLoanMiddleAudit);
			break;
		case edit:
			dzLoanMiddleAudit.initUpdateData(dzLoanMiddleAudit.getModifier(), dzLoanMiddleAudit.getModifierName());
			// 影响的记录数
			int updateCount = dzLoanMiddleAuditMapper.updateByPrimaryKeySelective(dzLoanMiddleAudit);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dzLoanMiddleAudit.initDeleteData(dzLoanMiddleAudit.getModifier(), dzLoanMiddleAudit.getModifierName());
			// 影响的记录数
			int deleteCount = dzLoanMiddleAuditMapper.updateByPrimaryKeySelective(dzLoanMiddleAudit);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DzLoanMiddleAudit> selectByExt(DzLoanMiddleAuditExt dzLoanMiddleAuditExt) {
		return dzLoanMiddleAuditMapper.selectByExt(dzLoanMiddleAuditExt);
	}
	
	@Override
	public int countByExt(DzLoanMiddleAuditExt dzLoanMiddleAuditExt){
		return dzLoanMiddleAuditMapper.countByExt(dzLoanMiddleAuditExt);
	}
	
	@Override
	public DzLoanMiddleAudit selectById(String id) {
		return dzLoanMiddleAuditMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dzLoanMiddleAuditMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DzLoanMiddleAudit dzLoanMiddleAudit) {
		return dzLoanMiddleAuditMapper.countRefined(dzLoanMiddleAudit);
	}

	@Override
	public DzLoanMiddleAudit selectRefined(DzLoanMiddleAudit dzLoanMiddleAudit) {
		return dzLoanMiddleAuditMapper.selectRefined(dzLoanMiddleAudit);
	}

	@Override
	public boolean repeat(DzLoanMiddleAudit dzLoanMiddleAudit) {
		int count = countRefined(dzLoanMiddleAudit);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dzLoanMiddleAudit.getId())) {
			return true;
		}
		DzLoanMiddleAudit dbItem = selectRefined(dzLoanMiddleAudit);
		if(null == dbItem) {
			return false;
		}
		return !dzLoanMiddleAudit.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dzLoanMiddleAuditMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public void confirmContract(List<DzLoanMiddleAudit> list, String busiId,
			AbstractSysUser currentUser) {
		if(list!=null &&list.size()>0){
			DzLoanMiddleAudit dzLoanMiddleAudit= list.get(0);
			dzLoanMiddleAudit.setTimeContractPrint(new Date());
			dzLoanMiddleAudit.setOperatorContractPrint(currentUser.getName());
			dzLoanMiddleAudit.initUpdateData(currentUser.getId(), currentUser.getName());
			int updateCount =dzLoanMiddleAuditMapper.updateByPrimaryKeySelective(dzLoanMiddleAudit);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
		}else{
			DzLoanMiddleAudit dzLoanMiddleAudit=new DzLoanMiddleAudit();
			dzLoanMiddleAudit.setTimeContractPrint(new Date());
			dzLoanMiddleAudit.setOperatorContractPrint(currentUser.getName());
			dzLoanMiddleAudit.initInsertData(currentUser.getId(), currentUser.getName());
			dzLoanMiddleAuditMapper.insert(dzLoanMiddleAudit);
		}
		
		YwBusiness ywBusiness= ywBusinessMapper.selectByPrimaryKey(busiId);
		if(ywBusiness!=null ){
			if(ywBusiness.getBusiStatus()!=null && ywBusiness.getBusiStatus().equals(BusiStatus.CHE_INFO_REPLAY_OK.getCode())){
				ywBusiness.setBusiStatus(BusiStatus.HETONG_CONSTRACT_OK.getCode());
				ywBusiness.initUpdateData(currentUser.getId(), currentUser.getName());
				int updateCount1=ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
				if(updateCount1 == 0){
					throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
				} else if(updateCount1 > 1){
					throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
				}
			}
		}
		
	}

	@Override
	public void modifyTime(DzLoanMiddleAudit dzLoanMiddleAudit) {
		dzLoanMiddleAudit.initUpdateData(dzLoanMiddleAudit.getModifier(), dzLoanMiddleAudit.getModifierName());
		// 影响的记录数
		int updateCount = dzLoanMiddleAuditMapper.updateByPrimaryKeySelective(dzLoanMiddleAudit);
		if(updateCount == 0){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		} else if(updateCount > 1){
			throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
		}
		YwBusiness ywBusiness= new YwBusiness();
		if(dzLoanMiddleAudit.getTimeContractFull()!=null){
			ywBusiness.setId(dzLoanMiddleAudit.getBusiId());
		    ywBusiness.setTimeContractFull(dzLoanMiddleAudit.getTimeContractFull());
			ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		}
//		DzLoanMiddleAuditSub dzLoanMiddleAuditSub=new DzLoanMiddleAuditSub();
//		dzLoanMiddleAuditSub.initInsertData(dzLoanMiddleAudit.getModifier(), dzLoanMiddleAudit.getModifierName());
//		dzLoanMiddleAuditSub.setBusiId(dzLoanMiddleAudit.getBusiId());
//		dzLoanMiddleAuditSub.setCusId(dzLoanMiddleAudit.getCusId());
//		dzLoanMiddleAuditSub.setSubmitType(dzLoanMiddleAudit.getTimeCName());
//		dzLoanMiddleAuditSubMapper.insert(dzLoanMiddleAuditSub);
	}

	@Override
	public void saveTime(DzLoanMiddleAudit dzLoanMiddleAudit, boolean saveSub) {
		dzLoanMiddleAudit.initInsertData(dzLoanMiddleAudit.getModifier(), dzLoanMiddleAudit.getModifierName());
		dzLoanMiddleAuditMapper.insert(dzLoanMiddleAudit);
		
		YwBusiness ywBusiness= new YwBusiness();
		if(dzLoanMiddleAudit.getTimeContractFull()!=null){
			ywBusiness.setId(dzLoanMiddleAudit.getBusiId());
		    ywBusiness.setTimeContractFull(dzLoanMiddleAudit.getTimeContractFull());
			ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		}
		if(saveSub) {
			DzLoanMiddleAuditSub dzLoanMiddleAuditSub=new DzLoanMiddleAuditSub();
			dzLoanMiddleAuditSub.initInsertData(dzLoanMiddleAudit.getModifier(), dzLoanMiddleAudit.getModifierName());
			dzLoanMiddleAuditSub.setBusiId(dzLoanMiddleAudit.getBusiId());
			dzLoanMiddleAuditSub.setCusId(dzLoanMiddleAudit.getCusId());
			dzLoanMiddleAuditSub.setSubmitType(dzLoanMiddleAudit.getTimeCName());
			dzLoanMiddleAuditSubMapper.insert(dzLoanMiddleAuditSub);
		}
			
	}

	@Override
	public void updateAll(AbstractSysUser currentUser,DzLoanMiddleAudit dzLoanMiddleAudit) {
		dzLoanMiddleAudit.setDpStatus(1);
		dzLoanMiddleAudit.initUpdateData(dzLoanMiddleAudit.getModifier(), dzLoanMiddleAudit.getModifierName());
		// 影响的记录数
		int updateCount = dzLoanMiddleAuditMapper.updateByPrimaryKey(dzLoanMiddleAudit);
		if(updateCount == 0){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		} else if(updateCount > 1){
			throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
		}
		
		
		YwBusiness ywBusiness=new YwBusiness();
        ywBusiness.setId(dzLoanMiddleAudit.getBusiId());
        ywBusiness.setTimeContractFull(dzLoanMiddleAudit.getTimeContractFull());
        ywBusiness.initUpdateData(ywBusiness.getModifier(), ywBusiness.getModifierName());
		// 影响的记录数
		int updateCount1 = ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		if(updateCount1 == 0){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		} else if(updateCount > 1){
			throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
		}
	}

	@Override
	public void updateByPrimaryKey(DzLoanMiddleAudit dzLoanMiddleAudit) {
		dzLoanMiddleAudit.setDpStatus(1);
		dzLoanMiddleAudit.initUpdateData(dzLoanMiddleAudit.getModifier(), dzLoanMiddleAudit.getModifierName());
		
		DzLoanMiddleAudit hisAudit = dzLoanMiddleAuditMapper.selectByPrimaryKey(dzLoanMiddleAudit.getId());
		//将原先未送到前端的字段赋值给 dzLoanMiddleAudit
		if(null != hisAudit) {
			if(StringUtils.isBlank(dzLoanMiddleAudit.getCcol1())) {
				dzLoanMiddleAudit.setCcol1(hisAudit.getCcol1());
			}
			if(StringUtils.isBlank(dzLoanMiddleAudit.getCcol2())) {
				dzLoanMiddleAudit.setCcol2(hisAudit.getCcol2());
			}
			if(StringUtils.isBlank(dzLoanMiddleAudit.getCcol3())) {
				dzLoanMiddleAudit.setCcol3(hisAudit.getCcol3());
			}
			if( null ==dzLoanMiddleAudit.getDcol1()) {
				dzLoanMiddleAudit.setDcol1(hisAudit.getDcol1());
			}
			if(null == dzLoanMiddleAudit.getDcol2()) {
				dzLoanMiddleAudit.setDcol2(hisAudit.getDcol2());
			}
			if(null == dzLoanMiddleAudit.getTimeContractFullStatus()) {
				dzLoanMiddleAudit.setTimeContractFullStatus(hisAudit.getTimeContractFullStatus());
			}
			if(null == dzLoanMiddleAudit.getTimeContractHandPrintStatus()) {
				dzLoanMiddleAudit.setTimeContractHandPrintStatus(hisAudit.getTimeContractHandPrintStatus());
			}
			if(null == dzLoanMiddleAudit.getTimeReceiveContractStatus()) {
				dzLoanMiddleAudit.setTimeReceiveContractStatus(hisAudit.getTimeReceiveContractStatus());
			}
			if(null != dzLoanMiddleAudit.getTimeSendToBankStatus()) {
				dzLoanMiddleAudit.setTimeSendToBankStatus(hisAudit.getTimeSendToBankStatus());
			}
		}
		
		// 影响的记录数
		int updateCount = dzLoanMiddleAuditMapper.updateByPrimaryKey(dzLoanMiddleAudit);
		if(updateCount == 0){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		} else if(updateCount > 1){
			throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
		}
		
	}
}
